package com.machine.filling.function.init.InputOilStationNum;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.http.response.OilMachineListResBean;
import com.machine.filling.net.http.response.OilStationInfoResBean;
import com.machine.filling.util.CustomToast;


import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/14 0014.
 * 油站信息查询
 */

public class InputOilStationNumPresenter implements InputOilStationNumContract.Presenter {
    private Context mContext;
    private InputOilStationNumContract.View mView;
    private String stationNum;//油站编号
    private String stationId;//油站id [查询商家信息后获取的]
    private String stationName;//油站名称
    private String machineId;//油机id
    private String clientId;//终端id

    public InputOilStationNumPresenter(Context mContext, InputOilStationNumContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {


    }

    /**
     * 查询商家信息
     */
    @Override
    public void searchStationInfoByNum() {
        stationNum = mView.getStationNum();
        if (TextUtils.isEmpty(stationNum)){
            CustomToast.showToast(mContext,"请输入油站编码");
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().getOilStationInfo(stationNum, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilStationInfoResBean bean = new Gson().fromJson(result,OilStationInfoResBean.class);
                if (bean.getResult()!=null) {
                    if (bean.getResult().getSeller() != null) {
                        stationId = bean.getResult().getSeller().getId();
                        stationName = bean.getResult().getSeller().getName();
                        mView.showStationInfo(bean.getResult().getSeller());
                    }else {
                        CustomToast.showToast(mContext,bean.getMsg());
                    }
                }else {
                    //极端
                    CustomToast.showToast(mContext,bean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 获取油机列表
     */
    @Override
    public void getOilMachineList() {
        mView.showProgress(null);
        if (TextUtils.isEmpty(stationId)){
            CustomToast.showToast(mContext,"请输入商家编码");
            return;
        }
        APIHttpManager.getInstance().getOilMachineList(stationId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilMachineListResBean bean = new Gson().fromJson(result,OilMachineListResBean.class);
                if (bean.getResult() != null){
                    if (bean.getResult().getMachines()!=null ){
                        mView.showOilMachineList(bean.getResult().getMachines());
                    }else {
                        CustomToast.showToast(mContext,bean.getMsg());
                    }
                }else {
                    CustomToast.showToast(mContext,bean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 绑定终端机
     */
    @Override
    public void bindOilMachineAndClient() {

        if (TextUtils.isEmpty(stationId)){
            CustomToast.showToast(mContext,"未获取到油站信息");
            return;
        }
        machineId = mView.getMachineId();
        if (TextUtils.isEmpty(machineId)){
            CustomToast.showToast(mContext,"请选择油机");
            return;
        }
        clientId = ClientInfoDao.getInstance().getModleByDeviceNum().getDeviceId();
        if (TextUtils.isEmpty(clientId)){
            CustomToast.showToast(mContext,"未获取到终端id");
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().bindClientAndOilMachine(stationId, machineId, clientId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                ClientInfoDao.getInstance().updataStationNum(stationNum);
                ClientInfoDao.getInstance().updataStationNum(stationId);
                ClientInfoDao.getInstance().updataStationName(stationName);
                ClientInfoDao.getInstance().updataMachineId(machineId);
                mView.showBindSuccess();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }


}
